package ro.jproject.imc.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import ro.jproject.imc.model.CnpVerification;
import ro.jproject.imc.model.DataBaseConnection;
import javax.swing.JTextField;
import java.awt.Color;

@SuppressWarnings("serial")
public class AddStudentGUI extends JFrame {

	private JFrame frame;
	private JPanel mainPanel;
	private JButton save;
	private JLabel lastNameLabel;
	private JLabel firstNameLabel;
	private JLabel lblStatus;
	private JLabel lblCnp;
	private JLabel lblUniversity;
	private JLabel lblRegistrationYear;
	private JLabel lblFaculty;
	private JLabel lblNewLabel;
	private JButton buttonExit;
	private String saveFirstName;
	private String saveLastName;
	private String saveStatus;
	private String cnp;
	private String faculty;
	private String specialization;
	private String registrationYear;
	private String yearsOfStudy;
	private String group;
	public static JComboBox<String> comboBoxGroup;
	private JTextField textFieldFirstName;
	private JTextField textFieldLastName;
	private JTextField textFieldCnp;
	@SuppressWarnings("rawtypes")
	private JComboBox comboBoxStatus;
	public static JComboBox<String> comboBoxSpecialization;
	public static JComboBox<String> comboBoxFaculty;
	private JComboBox<String> comboBoxYearsOfStudy;
	private DataBaseConnection dataBase = new DataBaseConnection();
	private JTextField textFieldRegistrationYear;

	/**
	 * Create the frame.
	 */
	@SuppressWarnings({"rawtypes", "unchecked" })
	public AddStudentGUI() {
		setTitle("Student Registration");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(500, 200, 441, 421);

		frame = new JFrame();
		frame.setBackground(Color.LIGHT_GRAY);
		mainPanel = new JPanel();
		save = new JButton("Save");
		firstNameLabel = new JLabel("First Name");
		lastNameLabel = new JLabel("Last Name");
		buttonExit = new JButton("Back");

		frame.getContentPane().add(mainPanel);
		mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		mainPanel.setLayout(null);

		save.setBounds(23, 342, 130, 28);
		mainPanel.add(save);

		setContentPane(mainPanel);

		firstNameLabel.setBounds(23, 29, 69, 14);
		mainPanel.add(firstNameLabel);

		lastNameLabel.setBounds(23, 66, 69, 14);
		mainPanel.add(lastNameLabel);

		buttonsAction();
		buttonExit.setBounds(261, 342, 137, 28);
		mainPanel.add(buttonExit);

		textFieldFirstName = new JTextField();
		textFieldFirstName.setBounds(127, 29, 271, 20);
		mainPanel.add(textFieldFirstName);
		textFieldFirstName.setColumns(10);

		textFieldLastName = new JTextField();
		textFieldLastName.setBounds(127, 66, 271, 20);
		mainPanel.add(textFieldLastName);
		textFieldLastName.setColumns(10);

		lblStatus = new JLabel("Status");
		lblStatus.setBounds(23, 130, 46, 14);
		mainPanel.add(lblStatus);

		textFieldCnp = new JTextField();
		textFieldCnp.setBounds(127, 97, 271, 20);
		mainPanel.add(textFieldCnp);
		textFieldCnp.setColumns(10);

		lblCnp = new JLabel("CNP");
		lblCnp.setBounds(23, 91, 46, 28);
		mainPanel.add(lblCnp);

		comboBoxGroup = new JComboBox<String>();
		
		dataBase.connected();
		try {
			dataBase.queryShowGroupsDataBase();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		comboBoxGroup.setBounds(127, 286, 271, 20);
		mainPanel.add(comboBoxGroup);

		lblUniversity = new JLabel("Group");
		lblUniversity.setBounds(23, 289, 69, 14);
		mainPanel.add(lblUniversity);
		
		
		lblRegistrationYear = new JLabel("Registration Year");
		lblRegistrationYear.setBounds(23, 227, 103, 14);
		mainPanel.add(lblRegistrationYear);
		
		comboBoxFaculty = new JComboBox<String>();
		dataBase.connected();
		
		try {
			dataBase.queryShowFacultyDataBase();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		comboBoxFaculty.setBounds(127, 160, 271, 20);
		mainPanel.add(comboBoxFaculty);
		
		lblFaculty = new JLabel("Faculty");
		lblFaculty.setBounds(23, 160, 69, 14);
		mainPanel.add(lblFaculty);
		
		comboBoxSpecialization = new JComboBox<String>();
		dataBase.connected();
		try {
			dataBase.queryShowSpecializationDataBase();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		comboBoxSpecialization.setBounds(127, 191, 271, 20);
		mainPanel.add(comboBoxSpecialization);
		
		lblNewLabel = new JLabel("Specialization");
		lblNewLabel.setBounds(23, 192, 92, 14);
		mainPanel.add(lblNewLabel);
		
		String items[] = {"Select", "Student", "Graduated", "Master","Expelled"};
		comboBoxStatus = new JComboBox(items);
		comboBoxStatus.setBounds(127, 130, 271, 20);
		mainPanel.add(comboBoxStatus);
		
		textFieldRegistrationYear = new JTextField();
		textFieldRegistrationYear.setBounds(127, 224, 271, 20);
		mainPanel.add(textFieldRegistrationYear);
		textFieldRegistrationYear.setColumns(10);
		
		JLabel lblYearsOfStudy = new JLabel("Years of study");
		lblYearsOfStudy.setBounds(23, 261, 103, 14);
		mainPanel.add(lblYearsOfStudy);
		
		String yearsOfStudy[] = {"Select", "1", "2", "3", "4"};
		comboBoxYearsOfStudy = new JComboBox(yearsOfStudy);
		comboBoxYearsOfStudy.setBounds(127, 255, 271, 20);
		mainPanel.add(comboBoxYearsOfStudy);
		
	}

	void saveListener(ActionListener s) {
		save.addActionListener(s);
	}

	public void buttonsAction() {
		// save button
		save.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				saveFirstName = textFieldFirstName.getText();
				saveLastName = textFieldLastName.getText();
				saveStatus = comboBoxStatus.getSelectedItem().toString();
				cnp = textFieldCnp.getText();
				faculty = comboBoxFaculty.getSelectedItem().toString();
				specialization = comboBoxSpecialization.getSelectedItem().toString();
				registrationYear = textFieldRegistrationYear.getText();
				yearsOfStudy = comboBoxYearsOfStudy.getSelectedItem().toString();
				group = comboBoxGroup.getSelectedItem().toString();

				CnpVerification cnpValid = new CnpVerification();
				cnpValid.cnpVerif(cnp);

				DataBaseConnection connection = new DataBaseConnection();
				connection.connected();
				try {
					if (cnpValid.cnpVerif(cnp) == true) {
						connection.insertQueryStudents(saveFirstName, saveLastName, cnp, saveStatus);
						connection.insertQueryStudentsRegistration(saveFirstName, saveLastName, cnp, saveStatus, faculty, 
																   specialization, registrationYear,
																   yearsOfStudy, group);
						
						JOptionPane.showMessageDialog(frame,"Registration saved!");
						
						textFieldFirstName.setText("");
						textFieldLastName.setText("");
						comboBoxStatus.setSelectedIndex(0);
						textFieldCnp.setText("");
						comboBoxFaculty.setSelectedIndex(0);
						comboBoxSpecialization.setSelectedIndex(0);
						textFieldRegistrationYear.setText("");
						comboBoxYearsOfStudy.setSelectedIndex(0);
						comboBoxGroup.setSelectedIndex(0);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

		// exit button
		buttonExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				Ums frame = new Ums();
				frame.setVisible(true);
			}
		});
	}
}
